<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/18
 * Time: 9:26
 */

namespace App\Http\Controllers\Drivers\V2\Drivers;

use App\Http\Requests\DriverOthersRequest;
use App\Http\Requests\DriversRequests;
use App\Http\Controllers\Controller;
use App\Services\DriversService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DriversController extends Controller
{
    /**
     * 司机资料提交中途退出保存接口
     */
    public function drivers_ubmission_after(Request $request, DriversService $service, DriversRequests $driversRequests)
    {
        $data = $request->post();
        $data['types'] = $request->post('types', 0);  // 0 网约车 1 出租车
        if (!$driversRequests->scene('edit')->check($data)) {
            return $this->sendError($driversRequests->getError());
        }
        if ($service->drivers_ubmission_after($data)) {
            return $this->sendSuccess('OK');
        };
        return $this->sendError('error');
    }

    /**
     * @param Request $request
     * @param DriversService $service
     * 继续填写,,司机资料回显
     */
    public function drivers_continue(Request $request, DriversService $service)
    {
        $types = (int)$request->post('types', 0); //0 w网约车  1 出粗车  默认网约车
        $info = $service->drivers($types);
        return $this->sendJson($info);

    }

    /**
     * 司机提交认证资料
     * 1,2,3.4
     * type  0 网约车  1 出租车
     */
    public function drivers_ubmission(Request $request, DriversService $driversService, DriversRequests $userValidate)
    {
        $data = $request->post();
        $data['types'] = (int)$request->post('types', 0);
        if (!$userValidate->scene('add')->check($data)) {
            return $this->sendError($userValidate->getError());
        }
        $res = $driversService->post_add(Auth::user(), $data);
        if ($res) {
            return $this->sendSuccess('提交成功耐心等待审核');
        } else {
            return $this->sendError('提交资料失败');
        }
    }

    /**
     * 审核进度查看
     */
    public function drivers_status()
    {
        $res = DriversService::status(Auth::user());
        return $this->sendJson($res);
    }

    /**
     * 业务类型
     */
    public function service(Request $request, DriversService $service)
    {
        $type = (int)$request->post('type', 0); //业务类型  0 网约车  1 出租车
        return $this->sendJson($service->service($type));
    }

    /**
     * @param Request $request
     * @param DriversService $service
     * @return DriversController|\Illuminate\Http\JsonResponse
     * 驾照类型
     */
    public function licence_list(Request $request, DriversService $service)
    {
        return $this->sendJson($service->licence_list());
    }

    /**
     * @param Request $request
     * @param DriversService $service
     * 绑定司机
     */
    public function bind_driver(Request $request, DriversService $service)
    {
        if ($service->bind_driver_car($request->post())) {
            return $this->sendSuccess('OK');
        }
        return $this->sendError('error');
    }

    /**
     * 添加紧急联系人
     */
    public function linkman_add(Request $request, DriversService $service)
    {
        $linkphone = (string)$request->post('linkphone');
        if (empty($linkphone)) return_bobc('手机号必传');
        $linkman = (string)$request->post('linkman');
        if ($service->linkman_add($linkphone, $linkman, Auth::user())) {
            return $this->sendSuccess('Ok');
        }
        return $this->sendError('error');
    }

    /**
     * 代驾提交认证资料
     */
    public function valet_driver_add(Request $request, DriversService $service, DriverOthersRequest $driverOthersRequest)
    {
        $data = $request->post();
        if (!$driverOthersRequest->check($data)) return $this->sendError($driverOthersRequest->getError());
        if ($service->valet_driver_add($data, Auth::user())) {
            return $this->sendSuccess('OK');
        }
        return $this->sendError('error');
    }

    /**
     * @param DriversService $service
     * @return DriversController|\Illuminate\Http\JsonResponse
     * 代驾资料回显数据
     */
    public function valet_driver_data(DriversService $service)
    {
        $info = $service->valet_driver_data(Auth::user());
        return $this->sendJson($info);
    }

    /**
     * @param Request $request
     * 地址表
     */
    public function region(Request $request, DriversService $service)
    {
        $pid = (int)$request->post('pid', 0);
        return $this->sendJson($service->region($pid));
    }

    /**
     * @param Request $request
     * @param DriversService $service
     * 报名地址:公司列表
     */
    public function company_list(Request $request, DriversService $service)
    {
        $city_id = (int)$request->post('city_id'); //城市ID
        return $this->sendJson($service->company_list($city_id));
    }
}
